home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / toolhelp.zip / TEST2.ASM < prev    next >
Assembly Source File  |  1991-05-09  |  7KB  |  211 lines

  1. ;***************************************************************************
  2. ;*  TEST2.ASM
  3. ;*
  4. ;*      Assembly support routines for the TOOLHELPER test program
  5. ;*
  6. ;***************************************************************************
  7.  
  8. ;** Set up values for cMacros
  9. ?PLM    =       1                       ;PASCAL-style parameter handling
  10. ?WIN    =       0                       ;No Windows prolog/epilog
  11.  
  12. ;** Symbols
  13. FAULT_1         EQU     2001
  14. FAULT_2         EQU     2002
  15. FAULT_3         EQU     2003
  16. FAULT_4         EQU     2004
  17. FAULT_5         EQU     2005
  18. FAULT_6         EQU     2006
  19. FAULT_7         EQU     2007
  20. FAULT_8         EQU     2008
  21.  
  22. .286c
  23.  
  24.         INCLUDE cMacros.INC
  25.         INCLUDE TOOLHELP.INC
  26.  
  27. sBegin  DATA
  28.  
  29. ODSTest DB      'THTest: Test Output Debug String', 0dh, 0ah
  30.  
  31. sEnd
  32.  
  33. ;** Functions
  34. ?PLM = 0
  35. externNP MyCFaultHandler
  36. ?PLM = 1
  37. externFP TerminateApp
  38. externFP OutputDebugString
  39. externFP GetWindowWord
  40.  
  41. sBegin  CODE
  42.         ASSUMES cs,CODE
  43.  
  44. ;  Fault
  45. ;
  46. ;       Causes one of three faults:
  47. ;               FAULT_1 --> GP Fault
  48. ;               FAULT_2 --> Illegal opcode
  49. ;               FAULT_3 --> Divide by zero
  50. ;               FAULT_4 --> Int 1
  51. ;               FAULT_5 --> Int 3
  52. ;               FAULT_6 --> RIP
  53. ;               FAULT_7 --> OutputDebugString
  54. ;               FAULT_8 --> Stack Fault
  55.  
  56. cProc   Fault, <PUBLIC,NEAR>, <si,di,ds>
  57.         parmW   wType
  58. cBegin
  59.         mov     ax,wType                ;Get the type
  60.         cmp     ax,FAULT_1              ;GP Fault?
  61.         je      GPFault                 ;Yes
  62.         cmp     ax,FAULT_2              ;Illegal opcode?
  63.         je      IllOpcode               ;Yes
  64.         cmp     ax,FAULT_3              ;Divide by zero?
  65.         je      DivZero                 ;Yes
  66.         cmp     ax,FAULT_4              ;Int 1?
  67.         je      Int1
  68.         cmp     ax,FAULT_5              ;Int 3?
  69.         je      Int3
  70.         cmp     ax,FAULT_6              ;RIP?
  71.         je      Rip
  72.         cmp     ax,FAULT_7              ;ODS?
  73.         je      ODS
  74.         cmp     ax,FAULT_8              ;Stack Fault?
  75.         je      StackFault
  76.         jmp     F_End                   ;Not recognized, get out
  77.  
  78.         ;** Produce GP Fault
  79. GPFault:
  80.         mov     cs:[0],BYTE PTR 55h     ;Can't write to CS
  81.         jmp     F_End                   ;If restarted
  82.  
  83.         ;** Illegal opcode
  84. IllOpcode:
  85.         DB      0c4h                    ;LES Opcode
  86.         DB      0c0h                    ;  LES AX,AX (should make #UD Excep)
  87.         jmp     F_End                   ;In case it's not
  88.  
  89.         ;** Divide by zero
  90. DivZero:
  91.         xor     bx,bx                   ;Get a zero
  92.         div     bx                      ;Divide by it
  93.         jmp     F_End                   ;Done
  94.  
  95. Int1:   int     1                       ;Cause trace interrupt
  96.         jmp     F_End                   ;Get out
  97.  
  98. Int3:   int     3                       ;Cause software int
  99.         jmp     F_End
  100.  
  101. Rip:    mov     ax,-1                   ;Bad handle
  102.         cCall   GetWindowWord, <ax,ax>  ;Guaranteed RIP
  103.         jmp     F_End
  104.  
  105. ODS:    mov     ax,OFFSET ODSTest       ;Test string
  106.         cCall   OutputDebugString, <ds,ax>
  107.         jmp     F_End
  108.  
  109. StackFault:
  110.         mov     ax,ss:[bp+7fffh]        ;Causes stack fault
  111.         jmp     F_End
  112.  
  113. F_End:
  114.  
  115. cEnd
  116.  
  117.  
  118. ;  MyFaultHandler
  119. ;       Handles all faults passed to it.  Makes the processing ready
  120. ;       for a C function, then calls it.  The handler is reentrant
  121. ;       The entry frame looks like this:
  122. ;               ------------
  123. ;        BP---->|  Old BP  |  [BP + 00h]
  124. ;               |  Ret IP  |  [BP + 02h]
  125. ;               |  Ret CS  |  [BP + 04h]
  126. ;               |    AX    |  [BP + 06h]
  127. ;               |Exception#|  [BP + 08h]
  128. ;               |  Handle  |  [BP + 0Ah]
  129. ;               |    IP    |  [BP + 0Ch]
  130. ;               |    CS    |  [BP + 0Eh]
  131. ;               |   Flags  |  [BP + 10h]
  132. ;               ------------
  133. ;
  134.  
  135. cProc   MyFaultHandler, <FAR,PUBLIC>
  136. cBegin  NOGEN
  137.  
  138.         push    bp                      ;Make a stack frame
  139.         mov     bp,sp
  140.         pusha                           ;Save all registers
  141.         push    ds
  142.         push    es
  143.  
  144.         ;** Get instance data segment and prepare for C call
  145.         mov     ds,ax                   ;Since this function uses
  146.                                         ;  MakeProcInstance(), AX has the
  147.                                         ;  DS value in it.
  148.  
  149.         ;** Call the C function:  It returns 0 to nuke the app,
  150.         ;*      1 to restart the instruction, 2 to chain on
  151.         ;*      The entry frame to the function looks like this:
  152.         ;*              ------------
  153.         ;*       BP---->|  Old BP  |  [BP + 00h] <-- Added by C routine
  154.         ;*              |    ES    |  [BP + 02h]
  155.         ;*              |    DS    |  [BP + 04h]
  156.         ;*              |    DI    |  [BP + 06h]
  157.         ;*              |    SI    |  [BP + 08h]
  158.         ;*              |    BP    |  [BP + 0Ah]
  159.         ;*              | Dummy SP |  [BP + 0Ch]
  160.         ;*              |    BX    |  [BP + 0Eh]
  161.         ;*              |    DX    |  [BP + 10h]
  162.         ;*              |    CX    |  [BP + 12h]
  163.         ;*              |    AX    |  [BP + 14h]
  164.         ;*   Old BP---->|Old,Old BP|  [BP + 16h]
  165.         ;*              |  Ret IP  |  [BP + 18h]
  166.         ;*              |  Ret CS  |  [BP + 1Ah]
  167.         ;*              |    AX    |  [BP + 1Ch]
  168.         ;*              |Exception#|  [BP + 1Eh]
  169.         ;*              |  Handle  |  [BP + 20h]
  170.         ;*              |    IP    |  [BP + 22h]
  171.         ;*              |    CS    |  [BP + 24h]
  172.         ;*              |   Flags  |  [BP + 26h]
  173.         ;*              ------------
  174.         ;**
  175.         cCall   MyCFaultHandler
  176.  
  177.         ;** Decode the return value
  178.         or      ax,ax                   ;Check for zero
  179.         jz      MFH_NukeApp             ;Nuke it
  180.         dec     ax                      ;Check for 1
  181.         jz      MFH_Restart             ;Restart instruction
  182.  
  183. MFH_ChainOn:
  184.         pop     es                      ;Chain on to next fault handler
  185.         pop     ds
  186.         popa
  187.         pop     bp
  188.         retf
  189.  
  190. MFH_Restart:
  191.         pop     es                      ;Clear stack
  192.         pop     ds
  193.         popa
  194.         pop     bp
  195.         add     sp,10                   ;Clear the return stuff
  196.         iret                            ;Restart instruction
  197.  
  198. MFH_NukeApp:
  199.         pop     es                      ;Clear stack
  200.         pop     ds
  201.         popa
  202.         pop     bp
  203.         add     sp,10                   ;Point to IRET frame
  204.         cCall   TerminateApp, <0,NO_UAE_BOX>
  205.  
  206. cEnd    NOGEN
  207.  
  208. sEnd
  209.  
  210.         END
  211.